#include "typedef.h"

// floppy base address
#define 	FD_PRIMARY_BASE 		0x03F0 
#define 	FD_SECONDARY_BASE 	0x0370 


//  floppy registers
#define 	FD_SRA			 0x00  // status register a
#define	FD_SRB	 		 0x01  // status register b
#define	FD_DOR			 0x02  // digital output register
#define	FD_TDR	 		 0x03  // tape drive register
#define	FD_MSR	 		 0x04  // main status register 
#define	FD_DRS			 0x04  // data rate select register 
#define	FD_DR 			 0x05  // data (FIFO)
#define	FD_DIR			 0x07  // digital input register
#define	FD_CCR	 		 0x07  // configuration control register


// floppy commands 
#define 	FIX_DRIVE_DATA 		0x03 
#define 	CHECK_DRIVE_STATUS	0x04 
#define 	CALIBRATE_DRIVE 		0x07 
#define 	CHECK_INTERRUPT_STATUS 	0x08 
#define 	FORMAT_TRACK 		0x4D 
#define 	READ_SECTOR			0x66 
#define 	READ_DELETE_SECTOR 	0xCC 
#define	        READ_SECTOR_ID 		0x4A 
#define 	READ_TRACK 			0x42 
#define	        SEEK_TRACK 			0x0F 
#define 	WRITE_SECTOR		0xC5 
#define 	WRITE_DELETE_SECTOR	0xC9 


// status register masks for MSR
#define	CMD_BUSY				0x10
#define	DR0_BUSY				0x01
#define	DR1_BUSY				0x02
#define	DR2_BUSY				0x04
#define	DR3_BUSY				0x08


// this is where BIOS stores disk parameters
#define 	DISK_PARAMETER_ADDRESS 		0x000fefc7


typedef struct { 
  u8 steprate_headunload;
  u8 headload_ndma; 
  u8 motor_delay_off; /*specified in clock ticks*/ 
  u8 bytes_per_sector; 
  u8 sectors_per_track; 
  u8 gap_length; 
  u8 data_length; /*used only when bytes per sector == 0*/ 
  u8 format_gap_length; 
  u8 filler; 
  u8 head_settle_time; /*specified in milliseconds*/ 
  u8 motor_start_time; /*specified in 1/8 seconds*/ 
}__attribute__ ((packed)) floppy_params_t; 


void fd_init();
//void fd_test(u8*);
void fd_motor_on(u32, u8);
void fd_motor_off(u8);
void reset_floppy_controller(u32, u8);
void configure_drive(u32, u8);
void calibrate_drive(u32, u8);
void check_interrupt_status(u32, u8*, u8*);
void wait_floppy_data(u32);
void write_floppy_command(u32, u8);
void seek_track(u8, u8, u8);
void read_sector(u8, u8, u8, u8, u8*);
void read_result_phase();
void wait_floppy_ready(u32);
